package com.nexgo.payment.trans.operator.edit;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.OperatorInfo;
import com.nexgo.payment.util.EncryptUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 编辑操作员实现
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年09月20日
 */
public class OperatorEditPresenter implements OperatorEditContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(OperatorEditPresenter.class.getSimpleName());

    private OperatorEditContract.View mView;
    private DataSource mRepository;
    private String mOperatorNo;

    public OperatorEditPresenter(@NonNull OperatorEditContract.View view, @NonNull DataSource repository, @NonNull String operatorNo) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        mOperatorNo = checkNotNull(operatorNo);
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        postMainThread(() -> mView.showOperatorNo(mOperatorNo));
    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void editOperator(String newPassword, String confirmPassword) {
        if (TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirmPassword)) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.input_can_no_be_empty)));
            return;
        }
        if (newPassword.length() != 4) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.err_input)));
            postMainThread(() -> mView.showClearNewPassword());
            return;
        }
        if (confirmPassword.length() != 4) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.err_input)));
            postMainThread(() -> mView.showClearConfirmPassword());
            return;
        }
        //判断两次输入是否一致
        if (!newPassword.equals(confirmPassword)) {
            postMainThread(() -> mView.showTips(App.sContext.getString(R.string.disagree_password)));
            postMainThread(() -> mView.showClearNewPassword());
            postMainThread(() -> mView.showClearConfirmPassword());
            return;
        }
        OperatorInfo info = mRepository.getOperator(mOperatorNo);
        if (info == null) {
            postMainThread(() -> postMainThread(() -> mView.showTips(App.sContext.getString(R.string.operator_not_exist))));
            return;
        }
        info.setPassword(EncryptUtils.encryptSHA256ToString(newPassword));
        mRepository.updateOperatorInfo(info);
        postMainThread(() -> mView.showTips(App.sContext.getString(R.string.password_update_success)));
        postMainThread(() -> mView.showSuccess());
    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        runnable.run();
    }
}
